﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace Truron.Purchase.Service.DbContext
{
    public class DapperHelper
    {
        /// 数据库连接名
        private static string _connection = "";
        public readonly static DapperHelper Instance = new DapperHelper();
        /// <summary>
        /// 私有构造方法，使外界不能创建该类的实例，以便实现单例模式
        /// </summary>
        private DapperHelper()
        {
            var Configuration = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json", optional: true)
               .Build();
            _connection = Configuration.GetSection("AppSettings:ConnectionString").Value;
        }
        /// <summary>
        /// 创建数据库连接对象并打开链接
        /// </summary>
        /// <returns></returns>
        public IDbConnection OpenCurrentDbConnection()
        {
            IDbConnection dbConnection = new SqlConnection(_connection);
            dbConnection.Open();
            return dbConnection;
        }
    }
}
